package com.qcxk.yuqian.system.utils;

import java.security.SecureRandom;
import java.util.Random;

public class ValidateCodeUtil {
    private static final String SYMBOLS = "0123456789"; // 数字
    private static final Random RANDOM = new SecureRandom();

    /**
     * 获取长度为 6 的随机数字
     *
     * @return 随机数字
     */
    public static String getNonce_str_six() {

        char[] nonceChars = new char[6];

        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }

        return new String(nonceChars);
    }

    /**
     * 获取长度为 4 的随机数字
     *
     * @return 随机数字
     */
    public static String getNonce_str_four() {

        char[] nonceChars = new char[4];

        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }

        return new String(nonceChars);
    }

    /**
     * 获取长度为 4 的随机数字
     *
     * @return 随机数字
     */
    public static String getNonce_str(Integer length) {

        char[] nonceChars = new char[length];

        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }

        return new String(nonceChars);
    }
}
